﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TracyGUI.Controller
{
    class UIUpdateHelper
    {
        /// <summary>
        /// Update UI in batches
        /// </summary>
        /// <typeparam name="T">Data Model Type</typeparam>
        /// <param name="numToUpdateEachTime">How many items to update each time</param>
        /// <param name="input">Items to update</param>
        /// <param name="updateUIOperation">The UI update operation</param>
        /// <param name="stopCondition"></param>
        public static void UpdateUIinTurn<T>(int numToUpdateEachTime, List<T> input, Action<List<T>> updateUIOperation, Func<bool> stopCondition)
        {
            List<T> tempList = new List<T>();
            for (int i = 0; i < input.Count; i++)
            {
                if (!stopCondition())
                {
                    tempList.Add(input[i]);
                    if (tempList.Count >= numToUpdateEachTime || i == input.Count - 1)
                    {
                        updateUIOperation(tempList);
                        tempList.Clear();
                    }
                }
            }
        }
    }
}
